﻿using System;
using System.Web.UI.WebControls;
using eCommerce.core.Helpers;
using eCommerce.db.BORespository;
using eCommerce.core;
using System.Configuration;

namespace eCommerce.web.Controls.Product
{
    public partial class Detail : System.Web.UI.UserControl
    {        
        protected int CategoryId;        
        protected string Title;
        protected string Description;
        protected string Content;
        protected string Images;
        protected string UnitPrice;
        protected string SpecialPrice;
        protected string strZoomImg = "<a href=\"{0}\" class=\"jqzoom\" rel='gal1' title=\"KMT\" ><img src=\"{1}\" style=\"border: 4px solid #e5e5e5;\" alt=\"\"/></a>";
        protected string strMainImg = "<li><a class=\"zoomThumbActive\" href='javascript:void(0);' rel=\"{0}\"><img src='{1}' alt=\"\"/></a></li>";
        protected string strImg = "<li><a {2} href='javascript:void(0);' rel=\"{0}\"><img src='{1}' alt=\"\"/></a></li>";
        protected string Img1, Img2, name;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var productId = Utils.GetIdFromRequestString(Utils.ProductId, 0);
                name = Utils.GetIdFromRequestString(Utils.Name, string.Empty);
                if (productId > 0)
                {
                    GetProductDetail(productId);
                    var cate = CategoryBo.GetByUrl(name);
                    Page.RegisterHiddenField("hidCategoryId", cate.CategoryId.ToString());

                    
                }

                
            }
        }

        private void GetProductDetail(int productId)
        {
            try
            {
                var o = ProductBo.GetById(productId);
                if (o != null)
                {
                    Title           = string.Format("{0} - {1}", o.Code, o.Name);
                    Description     = o.Description;
                    Content         = o.Content;                    
                    UnitPrice       = Utils.FormatPrice(Utils.ToDecimal(o.UnitPrice));
                    SpecialPrice    = Utils.FormatPrice(Utils.ToDecimal(o.SpecialPrice));
                    CategoryId      = o.CategoryId;

                    Related1.ProductID = o.ProductId;
                    Related1.CategoryID = o.CategoryId;

                    if(!name.Contains("so-mi"))
                    {
                         divDesc.Visible = true;
                        if (!string.IsNullOrEmpty(Description))
                        {
                            ltrDesc.Text = Description;
                        }
                    }
                    

                    var lstImage = ImageBo.GetByProductId(productId);
                    if (lstImage != null && lstImage.Count > 0)
                    {
                        //rptImage.DataSource = lstImage;
                        //rptImage.DataBind();

                        //rptThumbImage.DataSource = lstImage;
                        //rptThumbImage.DataBind();
                    }
                }

                var ImageList = ImageBo.GetByProductId(productId);
                if (ImageList != null)
                {
                    for (int i = 0; i < ImageList.Count; i++)
                    {

                        Img1 = ConfigurationManager.AppSettings["Thumbnail"] + string.Format("/Thumb.ashx?imgPath={0}&tid={1}&size=50", ImageList[i].ImagePath.ToString(), (int)SettingType.Image);
                        Img2 = ConfigurationManager.AppSettings["Thumbnail"] + string.Format("/Thumb.ashx?imgPath={0}&tid={1}&size=800", ImageList[i].ImagePath.ToString(), (int)SettingType.Image);
                        string Img308 = ConfigurationManager.AppSettings["Thumbnail"] + string.Format("/Thumb.ashx?imgPath={0}&tid={1}&size=400", ImageList[i].ImagePath.ToString(), (int)SettingType.Image);
                        string rel = "{gallery: 'gal1', smallimage: '" + Img308 + "',largeimage: '" + Img2 + "'}";
                        if (i == 0)
                        {
                            ltrZoomImg.Text = string.Format(strZoomImg, Img2, Img308, ImageList[i].Description);
                            //ltrMainImg.Text = string.Format(strMainImg, Img1, Img2);
                            ltrMainImg.Text = string.Format(strImg, rel, Img2,"class=\"zoomThumbActive\"");
                        }
                        else if (i > 0)
                            ltrImg.Text += string.Format(strImg, rel, Img2,string.Empty);
                    }

                }

                var ListProperty = ProductBo.GetListProperty(productId);
                if (ListProperty.Count > 0)
                {
                    rptProperty.DataSource = ListProperty;
                    rptProperty.DataBind();
                }
                else
                {
                    tabwrapper.Visible = false;
                }
            }
            catch(Exception ex) { }
        }    
    }
}